<?php

class Admin_userController extends Admin_baseController {

	public function listAction() {
		$page = Request::getParams('page', 1);
		$pageSize = Request::getParams('pageSize', 20);
		$where = $value = $url = array();
		$nickname = Request::getParams('nickname');
		$mobile = Request::getParams('mobile');
		if ($nickname) {
			$where[] = 'nick_name like ?';
			$value[] = '%' . $nickname . '%';
			$url['nickname'] = $nickname;
		}
		if ($mobile) {
			$where[] = 'mobile=?';
			$value[] = $mobile;
		}


		$total = self::model('users')->count('id', $where, $value);
		$list = self::model('users')->limit(($page - 1) * $pageSize, $pageSize)->order('id desc')->getAll($where, $value);
		$this->assign(array(
			'page' => $page,
			'pageSize' => $pageSize,
			'nickname' => $nickname,
			'mobile' => $mobile,
			//'status' => $status,
			'statusList' => self::model('user')->getAllStatus(),
			'total' => $total,
			'list' => $list,
			'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl() . '/admin_user/list?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
		));
		$this->display();
	}

	public function viewAction() {
		$uid = Request::getParams('uid');
		$info=self::model('users')->getOne("id=?",$uid);
		$level=[1=>"青铜",2=>"白银",3=>"黄金",4=>"铂金",5=>"钻石"];
		$info["level_name"]=$level[$info["user_level"]];
		$this->assign('userInfo', $info);
		$this->display();
	}

	public function editAction() {
		$uid = Request::getParams('uid');
		$data = [];
		$data['nickname'] = Request::getParams('nickname');
		$data['avatar'] = Request::getParams('avatar');
		$data['mobile'] = Request::getParams('mobile');
		$data['points'] = intval(Request::getParams('points'));
		$data['password'] = Request::getParams('password');
		$data['fans_total'] = intval(Request::getParams('fans_total'));

		$info = [];
		if ($uid && !$info = self::model('user')->getOne('uid=?', $uid)) {
			Response::showMsg('信息查询失败，请稍后重试');
		}
		if (Request::isGet()) {
			$this->assign('userInfo', $info);
			$this->display();
			exit;
		}
//        $result = articleModel::validata($data);
		//        if($result!==true) {
		//            Response::ajax(NORMAL_ERROR, $result);
		//        }
		if ($data['nickname'] && ($data['nickname'] != $info['nickname'])) {
			if (self::model('user')->getOne('nickname=?', $data['nickname'])) {
				Response::ajax(NORMAL_ERROR, '您填写的用户昵称有误');
			}
		}
		if ($data['mobile'] && ($data['mobile'] != $info['mobile'])) {
			if (self::model('user')->getOne('mobile=?', $data['mobile'])) {
				Response::ajax(NORMAL_ERROR, '您填写的手机号有误');
			}
		}
		if ($data['password']) {
			if (!Validation::isLength($data['password'], 8, 20)) {
				Response::ajax(NORMAL_ERROR, '密码长度在8到20位之间');
			}
			$data['salt'] = Text::randString(5);
			$data['password'] = md5(md5($data['password']) . $data['salt']);
		} else {
			unset($data['password']);
		}

		if ($uid) {
//修改
			if (self::model('user')->updateById($uid, $data)) {
				Response::ajax(200, '修改成功');
			}
		}
		Response::ajax(NORMAL_ERROR, '数据处理失败，请稍后重试');
	}

	public function virtualListAction() {
		$page = Request::getParams('page', 1);
		$pageSize = Request::getParams('pageSize', 20);
		$where = $value = $url = array();
		$nickname = Request::getParams('nickname');
		$mobile = Request::getParams('mobile');
		$status = Request::getParams('status', 0);
		$where[] = 'type=?';
		$value[] = userModel::TYPE_VIRTUAL;
		if ($nickname) {
			$where[] = 'nickname like ?';
			$value[] = '%' . $nickname . '%';
			$url['nickname'] = $nickname;
		}
		if ($mobile) {
			$where[] = 'mobile=?';
			$value[] = $mobile;
		}
		if ($status) {
			$where[] = 'status=?';
			$value[] = $status;
			$url['status'] = $status;
		}
		$where[] = 'type =?';
		$value[] = 2;
		$total = self::model('user')->count('uid', $where, $value);
		$this->assign(array(
			'page' => $page,
			'pageSize' => $pageSize,
			'nickname' => $nickname,
			'mobile' => $mobile,
			'status' => $status,
			'statusList' => self::model('user')->getAllStatus(),
			'total' => $total,
			'list' => self::model('user')->limit(($page - 1) * $pageSize, $pageSize)->order('uid desc')->getAll($where, $value),
			'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl() . '/admin_user/virtualList?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
		));
		$this->display();
	}

	/**
	 * 后台添加虚拟账号
	 */
	public function addAction() {
		$uid = Request::getParams('uid');
		$data = [];
		$data['nickname'] = Request::getParams('nickname');
		$data['avatar'] = Request::getParams('avatar');
		$data['mobile'] = Request::getParams('mobile');
		$data['points'] = intval(Request::getParams('points'));
		$data['type'] = userModel::TYPE_VIRTUAL;
		$info = [];
		if ($uid && !$info = self::model('user')->getOne('uid=?', $uid)) {
			Response::showMsg('信息查询失败，请稍后重试');
		}
		if (Request::isGet()) {
			$this->assign('userInfo', $info);
			$this->display();
			exit;
		}
		if (!$data['nickname']) {
			Response::ajax(NORMAL_ERROR, '您填写的用户昵称有误');
		}
		if ($data['nickname'] && ($info && $data['nickname'] != $info['nickname'])) {
			if (self::model('user')->getOne('nickname=?', $data['nickname'])) {
				Response::ajax(NORMAL_ERROR, '您填写的用户昵称有误');
			}
		}
		if ($data['mobile'] && ($info && $data['mobile'] != $info['mobile'])) {
			if (self::model('user')->getOne('mobile=?', $data['mobile'])) {
				Response::ajax(NORMAL_ERROR, '您填写的手机号有误');
			}
		}

		if ($data['points'] < 0) {
			Response::ajax(NORMAL_ERROR, '积分填写有误,修改失败');
		}

		if ($uid) {
//修改
			if (self::model('user')->updateById($uid, $data)) {
				Response::ajax(200, '修改成功');
			}
		} else {
			$data['add_time'] = date("Y-m-d H:i:s", time());
			self::model('user')->insert($data);
			Response::ajax(200, 'success');
		}
		Response::ajax(NORMAL_ERROR, '数据处理失败，请稍后重试');
	}

	public function cardListAction() {
		$page = Request::getParams('page', 1);
		$pageSize = Request::getParams('pageSize', 20);
		$where = $value = $url = array();
		$title = Request::getParams('title');
		$orderNo = Request::getParams('orderNo');
		$cardId = Request::getParams('cardId');
		$payStatus = strval(Request::getParams('payStatus', -1));
		$where[] = 'order_type=?';
		$value[] = orderModel::CARD;
		$where[] = 'is_delete=?';
		$value[] = 0;
		$uid = intval(Request::getParams('uid'));
		if ($uid) {
			$where[] = 'uid=?';
			$value[] = $uid;
			$url['uid'] = $uid;
		} else {
			Response::showMsg('用户信息查看失败');
		}
		if ($orderNo) {
			$where[] = 'order_no like ?';
			$value[] = '%' . $orderNo . '%';
			$url['orderNo'] = $orderNo;
		}
		if ($cardId) {
			$where[] = 'type_id=?';
			$value[] = $cardId;
			$url['cardId'] = $cardId;
		}
		if ($title) {
			$where[] = 'title like ?';
			$value[] = '%' . $title . '%';
			$url['title'] = $title;
		}
		if ($payStatus != -1) {
			$where[] = 'pay_status=?';
			$value[] = $payStatus;
			$url['payStatus'] = $payStatus;
		}
		$total = self::model('order')->count('order_id', $where, $value);
		$this->assign(array(
			'page' => $page,
			'pageSize' => $pageSize,
			'orderNo' => $orderNo,
			'title' => $title,
			'uid' => $uid,
			'payStatus' => $payStatus,
			'cardId' => $cardId,
			'total' => $total,
			'list' => self::model('order')->limit(($page - 1) * $pageSize, $pageSize)->order('order_id desc')->getAll($where, $value),
			'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl() . '/admin_card/orderList?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
		));
		$this->display();
	}

	public function serverListAction() {
		$page = Request::getParams('page', 1);
		$pageSize = Request::getParams('pageSize', 20);
		$where = $value = $url = array();
		$city = Request::getParams('city');
		$title = Request::getParams('title');
		$orderNo = Request::getParams('orderNo');
		$sn = Request::getParams('sn');
		$payStatus = intval(Request::getParams('payStatus', -1));
		$orderStatus = intval(Request::getParams('orderStatus', -1));
		$serverId = intval(Request::getParams('serverId'));
		$shopId = intval(Request::getParams('shopId'));
		$uid = intval(Request::getParams('uid'));
		if ($uid) {
			$where[] = 'uid=?';
			$value[] = $uid;
			$url['uid'] = $uid;
		} else {
			Response::showMsg('用户信息查看失败');
		}
		if ($orderNo) {
			$where[] = 'order_no like ?';
			$value[] = '%' . $orderNo . '%';
			$url['orderNo'] = $orderNo;
		}
		if ($shopId) {
			$where[] = 'shop_id=?';
			$value[] = $shopId;
			$url['shopId'] = $shopId;
		}
		if ($title) {
			$where[] = 'title like ?';
			$value[] = '%' . $title . '%';
			$url['title'] = $title;
		}
		if ($sn) {
			$where[] = 'sn=?';
			$value[] = $sn;
		}
		if ($payStatus != -1) {
			$where[] = 'pay_status=?';
			$value[] = $payStatus;
			$url['payStatus'] = $payStatus;
		}
		if ($orderStatus != -1) {
			$where[] = 'order_status=?';
			$value[] = $orderStatus;
			$url['orderStatus'] = $orderStatus;
		}
		if ($serverId) {
			$where[] = 'server_id=?';
			$value[] = $serverId;
			$url['serverId'] = $serverId;
		}
		$total = self::model('server_order')->count('order_id', $where, $value);
		$this->assign(array(
			'page' => $page,
			'pageSize' => $pageSize,
			'orderNo' => $orderNo,
			'title' => $title,
			'payStatus' => $payStatus,
			'uid' => $uid,
			'sn' => $sn,
			'orderStatus' => $orderStatus,
			'serverId' => $serverId,
			'shopId' => $shopId,
			'city' => $city,
			'serverInfo' => $serverId ? self::model('server')->getOne('server_id=?', $serverId) : [],
			'payStatusList' => self::model('server_order')->getPayStatusList(),
			'orderStatusList' => self::model('server_order')->getOrderStatusList(),
			'serverList' => self::model('server')->getAll('is_delete=0 and parent_id=0'),
			'cityList' => self::model('region')->getAll('level=2'),
			'total' => $total,
			'list' => self::model('server_order')->limit(($page - 1) * $pageSize, $pageSize)->order('order_id desc')->getAll($where, $value),
			'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl() . '/admin_user/serverList?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml()
		));
		$this->display();
	}

	public function pointListAction() {
		$page = Request::getParams('page', 1);
		$pageSize = Request::getParams('pageSize', 20);
		$where = $value = $url = array();
		$uid = intval(Request::getParams('uid'));
		if ($uid) {
			$where[] = 'uid=?';
			$value[] = $uid;
			$url['uid'] = $uid;
		} else {
			Response::showMsg('用户信息查看失败');
		}
		$total = self::model('user_point')->count('id', $where, $value);
		$this->assign(array(
			'page' => $page,
			'pageSize' => $pageSize,
			'total' => $total,
			'uid' => $uid,
			'list' => self::model('user_point')->limit(($page - 1) * $pageSize, $pageSize)->order('id desc')->getAll($where, $value),
			'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl() . '/admin_user/pointList?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
		));
		$this->display();
	}

	public function goodsListAction() {
		$page = Request::getParams('page', 1);
		$pageSize = Request::getParams('pageSize', 20);
		$where = $value = $url = array();
		$title = Request::getParams('title');
		$province = strval(Request::getParams('province'));
		$city = strval(Request::getParams('city'));
		$area = strval(Request::getParams('area'));
		$shopId = intval(Request::getParams('shopId'));
		$uid = intval(Request::getParams('uid'));
		$orderStatus = intval(Request::getParams('orderStatus', -1));
		$payStatus = intval(Request::getParams('payStatus', -1));
		$type = intval(Request::getParams('type'));
		$orderNo = strval(Request::getParams('orderNo'));
		$uid = intval(Request::getParams('uid'));
		if ($uid) {
			$where[] = 'uid=?';
			$value[] = $uid;
			$url['uid'] = $uid;
		} else {
			Response::showMsg('用户信息查看失败');
		}
//        $where[] = 'is_delete=0';
		if ($title) {
			$where[] = 'title like ?';
			$value[] = '%' . $title . '%';
			$url['title'] = $title;
		}
		if ($payStatus != -1) {
			$where[] = 'pay_status=?';
			$value[] = $payStatus;
			$url['payStatus'] = $payStatus;
		}

		if ($orderNo) {
			$where[] = 'order_no=?';
			$value[] = $orderNo;
			$url['orderNo'] = $orderNo;
		}
		if ($orderStatus != -1) {
			$where[] = 'order_status=?';
			$value[] = $orderStatus;
			$url['orderStatus'] = $orderStatus;
		}
		$total = self::model('order')->count('id', $where, $value);
		$this->assign(array(
			'page' => $page,
			'pageSize' => $pageSize,
			'title' => $title,
			'total' => $total,
			'uid' => $uid,
			'orderNo' => $orderNo,
			'payStatusList' => self::model('order')->getPayStatusList(),
			'orderStatusList' => self::model('order')->getOrderStatusList(),
			'payStatus' => $payStatus,
			'orderStatus' => $orderStatus,
			'list' => self::model('order')->limit(($page - 1) * $pageSize, $pageSize)->order('id desc')->getAll($where, $value),
			'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl() . '/admin_user/goodsList?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
		));
		$this->display();
	}

	public function statusAction() {
		$uid = intval(Request::getParams('uid'));
		$status = intval(Request::getParams('status'));
		if (self::model('user')->updateById($uid, ['status' => $status])) {
			Response::ajax(200, '修改成功');
		}
		Response::ajax(NORMAL_ERROR, '修改失败,请稍后再试');
	}

	public function countAction() {
		$page = Request::get('page', 1);
		$page_size = Request::get('page_size', 30);
		$total = self::model('user')->countByDay();
		$this->assign(array(
			'list' => self::model('user')->groupByDay($page, $page_size),
			'pageHtml' => self::instance('Pagination')->page($page)->num($page_size)->total($total)->url('/admin_user/count?page=')->rendHtml(),
		));
		$this->display();
	}

	public function point_listAction() {
		$page = Request::getParams('page', 1);
		$page_size = Request::getParams('page_size', 20);
		$uid = Request::getParams('uid');
		$where = $value = $url = array();
		if ($uid) {
			$where[] = 'uid=?';
			$value[] = $uid;
		}
		$total = self::model('user_point')->count(null, $where, $value);
		$this->assign(array(
			'page' => $page,
			'page_size' => $page_size,
			'total' => $total,
			'uid' => $uid,
			'list' => self::model('user_point')->limit(($page - 1) * $page_size, $page_size)->order('id desc')->getAll($where, $value),
			'pageHtml' => self::instance('Pagination')->page($page)->num($page_size)->total($total)->url('/admin_point/list?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
		));
		$this->display();
	}
}
